package com.nice.nice201110.Task;


import com.nice.nice201110.entity.TButton;
import com.nice.nice201110.entity.TMenu;
import com.nice.nice201110.entity.TOrganization;
import com.nice.nice201110.entity.TUser;
import com.nice.nice201110.mapper.TButtonMapper;
import com.nice.nice201110.mapper.TMenuMapper;
import com.nice.nice201110.mapper.TOrganizationMapper;
import com.nice.nice201110.mapper.TUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

/**
 * @ClassName SystemStartTask
 * 项目启动任务--启动前执行业务代码
 **/
@Slf4j
@Component
@Order(24)
class BusinessStartCodeTask implements CommandLineRunner {

    @Autowired
    TUserMapper tUserMapper;

    @Autowired
    TOrganizationMapper tOrganizationMapper;

    @Autowired
    TMenuMapper tMenuMapper;

    @Autowired
    TButtonMapper tButtonMapper;

    Logger logger = LoggerFactory.getLogger(getClass());

    private TButton tButton;  //按钮
    private TMenu tMenu;  // 菜单
    @Override
    public void run(String... args) throws Exception {
        /**
         * 初始化后执行insert及delete
         * */
        /**
         *  默认1级组织添加
         * */
        TOrganization tOrganization = tOrganizationMapper.selectByMain();
        if(tOrganization==null) {
            tOrganization = new TOrganization();
            tOrganization.setName("管理系统");
            tOrganization.setOrderby(0.0);
            tOrganization.setStatus(0);
            tOrganizationMapper.insertSelective(tOrganization);
            logger.error("组织信息:插入默认系统组织成功.信息如下" + tOrganization.toString());
        }
        Integer tOrganizationId = tOrganization.getId();
        /**
         *  删除及插入用户
         * */
        TUser tUser = tUserMapper.selectNice();
        if(tUser==null) {
            tUser = new TUser();
            tUser.setLoginid("nice");
            tUser.setIsmanger(2);
            tUser.setDate(new Date());
            tUser.setName("系统管理员");
            tUser.setOrganizationid(tOrganizationId);
            tUser.setStatue(0);
            String password = DigestUtils.md5DigestAsHex("123456".getBytes()); // MD5加密
            tUser.setPassword(password);
            tUserMapper.insertSelective(tUser);
            logger.error("用户信息:插入nice用户成功.信息如下" + tUser.toString());
        }
        /**
         * 前端页面的数据灌入
         * */
        List<TMenu> tMenus = tMenuMapper.selectByMain("管理员设置",null);
        logger.error("查询菜单【管理员设置】.数量：" + tMenus.size());
        if(tMenus.size()<=0) {
            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(0.0);
            tMenu.setName("管理员设置");
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【管理员设置】.信息如下" + tMenu.toString());

            int parentid = tMenu.getId();
            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(1.0);
            tMenu.setName("用户信息");
            tMenu.setParentid(parentid);
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【用户信息】.信息如下" + tMenu.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("增加");
            tButton.setOrderby(0.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【用户信息-增加】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("删除");
            tButton.setOrderby(2.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【用户信息-删除】.信息如下" + tButton.toString());


            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("修改");
            tButton.setOrderby(1.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【用户信息-修改】.信息如下" + tButton.toString());

            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(2.0);
            tMenu.setName("组织信息");
            tMenu.setParentid(parentid);
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【组织信息】.信息如下" + tMenu.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("增加");
            tButton.setOrderby(0.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【组织信息-增加】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("删除");
            tButton.setOrderby(2.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【组织信息-删除】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("修改");
            tButton.setOrderby(1.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【组织信息-修改】.信息如下" + tButton.toString());

            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(3.0);
            tMenu.setName("角色信息");
            tMenu.setParentid(parentid);
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【角色信息】.信息如下" + tMenu.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("增加");
            tButton.setOrderby(0.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【角色信息-增加】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("删除");
            tButton.setOrderby(2.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【角色信息-删除】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("修改");
            tButton.setOrderby(1.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【角色信息-修改】.信息如下" + tButton.toString());

        }
        tMenus = tMenuMapper.selectByMain("数据字典",null);
        logger.error("查询菜单【数据字典】.数量：" + tMenus.size());
        if(tMenus.size()<=0) {
            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(1.0);
            tMenu.setName("数据字典");
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【数据字典】.信息如下" + tMenu.toString());

            int parentid = tMenu.getId();
            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(1.0);
            tMenu.setName("种族基础表");
            tMenu.setParentid(parentid);
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【种族基础表】.信息如下" + tMenu.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("增加");
            tButton.setOrderby(0.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【种族基础表-增加】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("删除");
            tButton.setOrderby(2.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【种族基础表-删除】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("修改");
            tButton.setOrderby(1.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【种族基础表-修改】.信息如下" + tButton.toString());

            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(2.0);
            tMenu.setName("症状基础表");
            tMenu.setParentid(parentid);
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【症状基础表】.信息如下" + tMenu.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("增加");
            tButton.setOrderby(0.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【症状基础表-增加】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("删除");
            tButton.setOrderby(2.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【症状基础表-删除】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("修改");
            tButton.setOrderby(1.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【症状基础表-修改】.信息如下" + tButton.toString());

        }
        tMenus = tMenuMapper.selectByMain("物种信息",null);
        logger.error("查询菜单【物种信息】.数量：" + tMenus.size());
        if(tMenus.size()<=0) {
            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(3.0);
            tMenu.setName("物种信息");
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【物种信息】.信息如下" + tMenu.toString());

            int parentid = tMenu.getId();
            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(1.0);
            tMenu.setName("信息列表");
            tMenu.setParentid(parentid);
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【信息列表】.信息如下" + tMenu.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("增加");
            tButton.setOrderby(0.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【物种信息-信息列表-增加】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("删除");
            tButton.setOrderby(2.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【物种信息-信息列表-删除】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("修改");
            tButton.setOrderby(1.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【物种信息-信息列表-修改】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("批量处理疾病及用药");
            tButton.setOrderby(3.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【物种信息-信息列表-批量处理疾病及用药】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("批量使用资产");
            tButton.setOrderby(4.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【物种信息-信息列表-批量使用资产】.信息如下" + tButton.toString());

        }
        tMenus = tMenuMapper.selectByMain("资产信息",null);
        logger.error("查询菜单【资产信息】.数量：" + tMenus.size());
        if(tMenus.size()<=0) {
            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(4.0);
            tMenu.setName("资产信息");
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【资产信息】.信息如下" + tMenu.toString());

            int parentid = tMenu.getId();
            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(1.0);
            tMenu.setName("资产列表");
            tMenu.setParentid(parentid);
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【资产列表】.信息如下" + tMenu.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("增加");
            tButton.setOrderby(0.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【资产列表-增加】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("删除");
            tButton.setOrderby(2.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【资产列表-删除】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("修改");
            tButton.setOrderby(1.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【资产列表-修改】.信息如下" + tButton.toString());

            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(2.0);
            tMenu.setName("资产入库");
            tMenu.setParentid(parentid);
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【资产入库】.信息如下" + tMenu.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("增加");
            tButton.setOrderby(0.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【资产入库-增加】.信息如下" + tButton.toString());

            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(3.0);
            tMenu.setName("资产出库");
            tMenu.setParentid(parentid);
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【资产出库】.信息如下" + tMenu.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("增加");
            tButton.setOrderby(0.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【资产出库-增加】.信息如下" + tButton.toString());

        }
        tMenus = tMenuMapper.selectByMain("药品信息",null);
        logger.error("查询菜单【药品信息】.数量：" + tMenus.size());
        if(tMenus.size()<=0) {
            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(5.0);
            tMenu.setName("药品信息");
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【药品信息】.信息如下" + tMenu.toString());

            int parentid = tMenu.getId();
            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(1.0);
            tMenu.setName("药品列表");
            tMenu.setParentid(parentid);
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【药品列表】.信息如下" + tMenu.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("增加");
            tButton.setOrderby(0.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【药品列表-增加】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("删除");
            tButton.setOrderby(2.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【药品列表-删除】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("修改");
            tButton.setOrderby(1.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【药品列表-修改】.信息如下" + tButton.toString());

            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(2.0);
            tMenu.setName("药品入库");
            tMenu.setParentid(parentid);
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【药品入库】.信息如下" + tMenu.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("增加");
            tButton.setOrderby(0.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【药品入库-增加】.信息如下" + tButton.toString());


            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(1.0);
            tMenu.setName("药品出库");
            tMenu.setParentid(parentid);
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【药品出库】.信息如下" + tMenu.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("增加");
            tButton.setOrderby(0.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【药品出库-增加】.信息如下" + tButton.toString());

        }
        tMenus = tMenuMapper.selectByMain("疾病信息",null);
        logger.error("查询菜单【疾病信息】.数量：" + tMenus.size());
        if(tMenus.size()<=0) {
            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(6.0);
            tMenu.setName("疾病信息");
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【疾病信息】.信息如下" + tMenu.toString());

            int parentid = tMenu.getId();
            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(1.0);
            tMenu.setName("疾病列表");
            tMenu.setParentid(parentid);
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【疾病列表】.信息如下" + tMenu.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("增加");
            tButton.setOrderby(0.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【疾病列表-增加】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("删除");
            tButton.setOrderby(2.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【疾病列表-删除】.信息如下" + tButton.toString());

            tButton = new TButton();
            tButton.setIsdelete(0);
            tButton.setMenuid(tMenu.getId());
            tButton.setName("修改");
            tButton.setOrderby(1.0);
            tButtonMapper.insertSelective(tButton);
            logger.error("按钮信息:插入按钮【疾病列表-修改】.信息如下" + tButton.toString());

        }
        tMenus = tMenuMapper.selectByMain("系统消息",null);
        logger.error("查询菜单【系统消息】.数量：" + tMenus.size());
        if(tMenus.size()<=0) {
            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(7.0);
            tMenu.setName("系统消息");
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【系统消息】.信息如下" + tMenu.toString());

            int parentid = tMenu.getId();
            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(1.0);
            tMenu.setName("操作日志");
            tMenu.setParentid(parentid);
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【操作日志】.信息如下" + tMenu.toString());

            tMenu = new TMenu(); //菜单
            tMenu.setIsdelete(0);
            tMenu.setOrderby(2.0);
            tMenu.setName("提醒消息");
            tMenu.setParentid(parentid);
            tMenuMapper.insertSelective(tMenu);
            logger.error("菜单信息:插入菜单【提醒消息】.信息如下" + tMenu.toString());

        }

    }
}